﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ManagerGame
{
    /// <summary>
    /// Basic company class of the game
    /// </summary>
    [Serializable]
    public class Company
    {
        /// <summary>
        /// this is needed for serializing
        /// </summary>
        public Company(){}

        private static int pubCounter = 1; // let's start from number one
        private static int drugJointCounter = 1;
        private static int pimpCaveCounter = 1;
        /// <summary>
        /// Constructor that increases the value of Id
        /// </summary>
        public Company(BusinessType type)
        {
            Happiness = Happiness.Content;
            Income = 500;
            Workers = 1;
            Risk = 1;
            Salary = 30;
            CompanyType = type;
            Id = SetID();
            //IncreaseCounter();
        }

        private void IncreaseCounter()
        {
            switch (this.CompanyType)
            {
                case BusinessType.Pub:
                    pubCounter++;
                    break;
                case BusinessType.PimpCave:
                    pimpCaveCounter++;
                    break;
                case BusinessType.DrugJoint:
                    drugJointCounter++;
                    break;
                default:
                    break;
            }
        }

        private int SetID()
        {
            int result;
            switch (this.CompanyType)
            {
                case BusinessType.Pub:
                    result = pubCounter;
                    pubCounter++;
                    return result;
                case BusinessType.PimpCave:
                    result = pimpCaveCounter;
                    pimpCaveCounter++;
                    return result;
                case BusinessType.DrugJoint:
                    result = drugJointCounter;
                    drugJointCounter++;
                    return result;
                default:
                    return 0;
            }
        }

        /// <summary>
        /// Reset counters.
        /// </summary>
        public void ResetCounters()
        {
            pubCounter = 1; 
            drugJointCounter = 1;
            pimpCaveCounter = 1;
        }

        public static void UpdateCounter(BusinessType type, int count)
        {
            // "count" is the the value now, so increase it by one -> it is the next id 
            count++;
            switch (type)
            {
                case BusinessType.Pub:
                    pubCounter = count;
                    break;
                case BusinessType.PimpCave:
                    pimpCaveCounter = count;
                    break;
                case BusinessType.DrugJoint:
                    drugJointCounter = count;
                    break;
            }
        }
        /// <summary>
        /// Business type
        /// </summary>
        public BusinessType CompanyType;

        private int _id;

        /// <summary>
        /// Company Id. 
        /// </summary>
        public int Id
        {
            get
            {
                return _id;
            }
             set
            {
                this._id = value;
            }
        }

        /// <summary>
        /// How happy the workers are.
        /// </summary>
        public Happiness Happiness { get; set; }
        /// <summary>
        /// How much salary the salary costs are per turn
        /// </summary>
        public int Salary { get; set; }
        /// <summary>
        /// Amount of workers
        /// </summary>
        public int Workers { get; set; }
        /// <summary>
        /// Income per turn
        /// </summary>
        public double Income { get; set; }
        /// <summary>
        /// Risk of getting caught
        /// </summary>
        public double Risk { get; set; }
        /// <summary>
        /// Active operations
        /// </summary>
        public List<Operation> Operations { get; set; }
        /// <summary>
        /// Have the workers recently enjoyed sauna?
        /// </summary>
        /// <returns>True, if the workers are still in awe</returns>
        public bool HaveSauna()
        {
            return Operations.Any(operation => operation.OperationType == OperationType.CompanySauna && operation.RoundsActive > 1);
        }

        /// <summary>
        /// For testing
        /// </summary>
        public bool OperationLaunched;

        public int WorkerAmountIndex;
        public int SalaryAmountIndex;
        public int SpecialTreatmentIndex
        {
            get
            {
                return OperationLaunched == false ? 0 : 1;
            }
            set { }
        }
        /// <summary>
        /// Just for testing
        /// </summary>
        /// <returns>String for testing purposes</returns>
        public string toString()
        {
            string specialTreatment = OperationLaunched ? " *" : "";

            // todo: "Pub" should be replaced with the real type enum...
            return this.CompanyType.ToString() + " #" + this.Id + ", workers: " + this.Workers + ", salary: " + this.Salary + specialTreatment;
        }
    }
}
